package com.prs.crm.action.fs;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.fs.BillType;
import com.prs.crm.service.fs.BillTypeService;
import com.prs.crm.service.sys.LogService;

public class BillTypeAction extends BaseAction {
	private BillType billType;
	private BillTypeService billTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;
	private String billTypeName;

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public String getBillTypeName() {
		return billTypeName;
	}

	public void setBillTypeName(String billTypeName) {
		this.billTypeName = billTypeName;
	}

	@Action("listBillType")
	public String listBillType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				BillType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getBillTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editBillType")
	public String editBillType() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveBillType", results = {
			@Result(name = "success", type = "redirect", location = "listBillType"),
			@Result(name = "error", type = "chain", location = "editBillType") })
	public String saveBillType() {
		if (billType.getName() == null || billType.getName().trim().equals("")) {
			this.addActionError("票据类型不能为空");
			return ERROR;
		}
		if(this.getLogService().getSearchName(BillType.class, billType.getName())){
			if(!(billTypeName.equals(billType.getName()))){
				this.addActionError("名称已存在");
				return ERROR;
			}
		}
		this.getBillTypeService().save(billType);
		this.setPrompt("提交票据类型成功");
		return SUCCESS;
	}

	@Action(value = "removeBillType", results = {
			@Result(name = "success", type = "redirect", location = "listBillType"),
			@Result(name = "error", type = "chain", location = "listBillType") })
	public String removeBillType() {
		if (ids == null) {
			this.addActionError("请选择要删除的票据类型");
			return ERROR;
		}
		this.getBillTypeService().batchRemove(ids);
		return SUCCESS;
	}

	public BillType getBillType() {
		return billType;
	}

	public void setBillType(BillType billType) {
		this.billType = billType;
	}

	public BillTypeService getBillTypeService() {
		return billTypeService;
	}

	public void setBillTypeService(BillTypeService billTypeService) {
		this.billTypeService = billTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

}
